import { getTemplateByType } from '@/utils/ejsTemplate.js'
import { formatJavaCode, formatXmlCode } from '@/utils/formatter.js'
import { ElMessage } from 'element-plus'
import { parseMysql, parseOracleOrPostgresql } from '@/utils/sqlParser.js'
import { commonConfig } from '@/utils/config.js'
import ejs from 'ejs'
import {
  camelCaseFirstChar2Lower,
  camelCaseFirstChar2Upper,
  convertColumn,
  formatDate,
  getEntityImportList, getXmlData
} from '@/utils/commonUtil.js'


/**
 * 输出result结果类型
 * @param that  vue中data的响应式对象
 * @param config 配置
 */
export function generateResult (that, config) {
  const result = ejs.render(getTemplateByType(that.fileType), {
    config
  })
  that.rightStr = formatJavaCode(result)
}

/**
 * 生成搜索按钮
 * @param that
 */
export function generateSearchButtons (that) {
  const template = getTemplateByType(that.fileType)
  that.rightStr = ejs.render(template, {})
}

export function parseTableInfo (that) {
  that.leftStr = that.leftStr.replaceAll('`', '')
  switch (that.dbType) {
    case 'mysql':
      that.tableInfo = parseMysql(that.leftStr)
      break
    case 'oracle':
    case 'postgresql':
      that.tableInfo = parseOracleOrPostgresql(that.leftStr)
      break
    default:
      ElMessage.error(commonConfig.future)
  }
}

export function convertTableInfo (that, config, typeMapping) {
  const tableInfo = that.tableInfo
  //=========处理字段============
  const columns = tableInfo.columns
  let columnList = []
  for (const item of columns) {
    columnList.push(convertColumn(config, typeMapping, item))
  }
  let primaryKey = tableInfo.primaryKey
  if (tableInfo.primaryKey) {
    const arr = columnList.filter(item => item.name === primaryKey)
    if (arr.length > 0) {
      primaryKey = arr[0]
    }
  } else {
    primaryKey = columnList[0]
  }
  //=========处理字段============
  let tableName = tableInfo.tableName
  if (config.tablePrefix) {
    tableName = tableName.replace(config.tablePrefix, '')
  }
  const table = {
    tableName: tableInfo.tableName,
    className: camelCaseFirstChar2Upper(tableName),
    firstLowerClassName: camelCaseFirstChar2Lower(tableName),
    tableComment: tableInfo.tableComment,
  }
  const simpleSuperClass = config.superClass.split('.')
  if (config.basePackage && !config.basePackage.endsWith('.')) {
    config.basePackage += '.'
  }
  config.date = formatDate(new Date())
  return {
    table,
    primaryKey,
    columnList,
    config,
    simpleSuperClass: simpleSuperClass[simpleSuperClass.length - 1]
  }
}

export function generateEntity (that, config) {
  const template = getTemplateByType(that.fileType)
  const data = {
    ...that.afterConvert,
    entityImportList: getEntityImportList(config),
    pattern: config.dateType === 'LocalDate' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss'
  }
  that.rightStr = formatJavaCode(ejs.render(template, data))
}

// export function generateMapper (that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatJavaCode(ejs.render(template, that.afterConvert))
// }
// export function generateController(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatJavaCode(ejs.render(template, that.afterConvert))
// }
// export function generateService(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatJavaCode(ejs.render(template, that.afterConvert))
// }
// export function generateImpl(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatJavaCode(ejs.render(template, that.afterConvert))
// }

export function generateJavaCode (that) {
  const template = getTemplateByType(that.fileType)
  that.rightStr = formatJavaCode(ejs.render(template, that.afterConvert))
}

export function generateXml (that) {
  that.rightStr = formatXmlCode(ejs.render(getTemplateByType(that.fileType), {
    ...that.afterConvert,
    xml: getXmlData(that.afterConvert, that.dbType, commonConfig.inForeachPrefix, commonConfig.suffix)
  }))
}

// export function generateMockJs(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = ejs.render(template, that.afterConvert)
// }
// export function generateApiJs(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = ejs.render(template, that.afterConvert)
// }

export function generateJs (that) {
  const template = getTemplateByType(that.fileType)
  that.rightStr = ejs.render(template, that.afterConvert)
}


// export function generateIndexVue(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatXmlCode(ejs.render(template, that.afterConvert))
// }
// export function generateFormVue(that) {
//   const template = getTemplateByType(that.fileType)
//   that.rightStr = formatXmlCode(ejs.render(template, that.afterConvert))
// }

export function generateXmlOrHtml (that) {
  const template = getTemplateByType(that.fileType)
  that.rightStr = formatXmlCode(ejs.render(template, that.afterConvert))
}
